/**
 * Created by  on 2017/8/30.
 */

var OrderViewer = {

    currentViewOrderId: 0,//当前显示品牌信息Id

    /**
     * 加载订单详情信息
     * @param id
     */
    loadOrderDetail: function (id) {
        OrderViewer.currentViewOrderId = id;

        var selector = "#page_order_detail";

        function callback(data, status, xhr) {
            if (status == "success") {
                $(selector).empty();
                $(selector).html(data);
                OrderViewer.showOrderDetail();
            } else {
                $(selector).html("错误信息: <br/>" + xhr.status + " " + xhr.statusText)
            }
        }

        var url = "/smAdmin/enterprise/order_detail?id=" + id;
        // 请求编辑框的页面数据库
        $.get(url, "", callback);
    },

    /**
     * 显示品牌列表信息
     */
    showPageOrderList: function () {
        $('#page_order_list').show();
        $('#page_order_detail').hide();
        OrderViewer.reloadOrderList();
    },

    /**
     * 显示品牌详情信息
     */
    showOrderDetail: function () {
        $('#page_order_list').hide();
        $('#page_order_detail').show();
    },

    /**
     * 重新加载品牌列表信息
     */
    reloadOrderList: function () {
        var filters = {"groupOp": "AND", "rules": []};
        var rules = filters.rules;
        var args = formserializeJson("order_list_form");
        for (var name in args) {
            var value = args[name];
            if (value) {
                rules.push({field: name, op: "cn", data: value});
            }
        }

        var postData = $("#order-grid-table").jqGrid("getGridParam", "postData");
        $.extend(postData, {filters: JSON.stringify(filters)});
        $("#order-grid-table").jqGrid("setGridParam", {search: true}).trigger("reloadGrid", [{page: 1}]);  //重载JQGrid
    },

    /**
     * 显示确认付款对话框
     */
    showCheckOrderDialog: function () {
        var layer = layui.layer;
        var that = this;
        //多窗口模式，层叠置顶
        layer.open({
            type: 1
            , title: '确认付款信息：'
            , area: ['500px', '350px']
            , shade: 0
            , maxmin: true
            , offset: "auto"
            , content: '<div class="alert alert-info bigger-110">如果已经收款，请按"确认"，确认后会给订单对应的产品增加溯码</div>'
            , btn: ['取消', '确认']
            , yes: function () {
                layer.closeAll();
            }
            , btn2: function () {
                OrderViewer.submitOrderPayOkResult();
                // layer.closeAll();
                return false;
            }
            , zIndex: layer.zIndex //重点1
            , success: function (layero) {
                layer.setTop(layero); //重点2
            }
        });
    },

    /**
     * 提交确认付款
     */
    submitOrderPayOkResult: function () {

        function callback(data, status, xhr) {
            if (status == "success") {
                if (data.result == 0) {
                    OrderViewer.loadOrderDetail(OrderViewer.currentViewOrderId);
                    layui.layer.closeAll();
                    alert("成功：" + data.reason);
                } else {
                    alert("错误：" + data.reason);
                }
            } else {
                var msg = "错误信息: <br/>" + xhr.status + " " + xhr.statusText;
                alert(msg);
            }
        }

        var url = "/smAdmin/enterprise/order_pay_ok";
        var post_data = {
            id: OrderViewer.currentViewOrderId,
        };
        $.get(url, post_data, callback, 'json');
    },

    // 创建新对象
    createNew: function () {
        var instance = {};

        return instance;
    },

};